{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using IPython Parallel as a joblib backend\n",
    "\n",
    "joblib 0.10 adds the ability to register a custom parallel backend.\n",
    "IPython 5.1 defines one such backend, so you can use IPython parallel with joblib.\n",
    "\n",
    "The simplest way to set this up is a single call:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ipyparallel as ipp\n",
    "ipp.register_joblib_backend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This registers the 'ipyparallel' backend with all the defaults."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " -1,\n",
       " -2,\n",
       " -3,\n",
       " -4,\n",
       " -5,\n",
       " -6,\n",
       " -7,\n",
       " -8,\n",
       " -9,\n",
       " -10,\n",
       " -11,\n",
       " -12,\n",
       " -13,\n",
       " -14,\n",
       " -15,\n",
       " -16,\n",
       " -17,\n",
       " -18,\n",
       " -19,\n",
       " -20,\n",
       " -21,\n",
       " -22,\n",
       " -23,\n",
       " -24,\n",
       " -25,\n",
       " -26,\n",
       " -27,\n",
       " -28,\n",
       " -29,\n",
       " -30,\n",
       " -31]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from joblib import Parallel, delayed\n",
    "\n",
    "def neg(x):\n",
    "    return -x\n",
    "\n",
    "Parallel(backend='ipyparallel')(delayed(neg)(i) for i in range(32))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also configure your own View, and register it explicitly, or even as the default:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ipyparallel as ipp\n",
    "rc = ipp.Client()\n",
    "rc[:].use_cloudpickle()\n",
    "view = rc.load_balanced_view()\n",
    "view.register_joblib_backend(make_default=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10625, 10621, 10647, 10637, 10622, 10620, 10642, 10631, 10625, 10621]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "Parallel()(delayed(os.getpid)() for i in range(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Joblib also provides a context manager for selecting a particular backend:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10622, 10620, 10642, 10631, 10622]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "even = rc.load_balanced_view(targets=rc.ids[::2])\n",
    "even.register_joblib_backend('even')\n",
    "\n",
    "from joblib import parallel_backend\n",
    "with parallel_backend('even'):\n",
    "    result = Parallel()(delayed(os.getpid)() for i in range(5))\n",
    "result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
